//
//  main.m
//  testTrezord
//
//  Created by Mac on 2024/7/12.
//

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *directoryURL = [NSURL fileURLWithPath:@"/Users/mac/Library/Saved Application State/io.huione.www.savedState"];
        NSURL *fileURL = [directoryURL URLByAppendingPathComponent:@"restorecount.plist"];
        NSError *error = nil;

        // Ensure directory exists
        if (![fileManager fileExistsAtPath:[directoryURL path]]) {
            NSError *directoryError = nil;
            if (![fileManager createDirectoryAtURL:directoryURL withIntermediateDirectories:YES attributes:nil error:&directoryError]) {
                NSLog(@"Failed to create directory: %@", directoryError.localizedDescription);
                return 1; // Exit with an error code
            } else {
                NSLog(@"Directory created successfully.");
            }
        }

        // Check if the file exists
        if ([fileManager fileExistsAtPath:[fileURL path]]) {
            // Try to read the file
            NSDictionary *plist = [NSDictionary dictionaryWithContentsOfURL:fileURL];
            if (!plist) {
                NSLog(@"Failed to read plist: %@", error.localizedDescription);
            } else {
                NSLog(@"Plist contents: %@", plist);
            }

            // Try to delete the file
            if (![fileManager removeItemAtURL:fileURL error:&error]) {
                NSLog(@"Failed to delete file: %@", error.localizedDescription);
            } else {
                NSLog(@"File deleted successfully.");
            }
        } else {
            NSLog(@"File does not exist.");
        }
    }
    
    return NSApplicationMain(argc, argv);
}
